package com.foosun.core.service;

import com.foosun.core.base.BaseDao;
import com.foosun.core.base.BaseService;
import com.foosun.core.base.PageData;
import com.foosun.core.entity.Resource;
import com.foosun.core.entity.ResourceGroup;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Nelin on 14-2-21.
 */
@Service
@Transactional
public class ResourceService extends BaseService<Resource> {

    @Override
    @javax.annotation.Resource(name = "resourceDao")
    public void setBaseDao(BaseDao<Resource> baseDao) {
        this.baseDao = baseDao;
    }

    @javax.annotation.Resource
    private ResourceGroupService resourceGroupService;


    @Override
    public PageData<Resource> findList(PageData<Resource> pageData) throws Exception {
        pageData = super.findList(pageData);
        List<Resource> resources = pageData.getResult();
        Map<Integer, ResourceGroup> groupMap = new HashMap<Integer, ResourceGroup>();
        if (resources != null) {
            for (Resource r : resources) {
                ResourceGroup group = groupMap.get(r.getGroupId());
                if (group == null) {
                    group = resourceGroupService.findById(r.getGroupId());
                    groupMap.put(r.getGroupId(), group);
                }
                r.setGroup(group);
            }
        }
        return pageData;
    }
}
